﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GetDataOpen
{
    public static class LogOperate
    {
        /// <summary>
        /// 写错误日志信息
        /// </summary>
        /// <param name="txt"></param>
        public static void WriteLog(string txt)
        {
            try
            {
                string path = Application.StartupPath + @"\log\" + DateTime.Now.ToString("yyyy-MM-dd") + @"\";

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);

                }
                path += DateTime.Now.ToString("yyyyMMdd") + "-" + DateTime.Now.ToString("HH") + ".txt";

                if (!File.Exists(path))
                {
                    using (FileStream fscreate = new FileStream(path, FileMode.Create))
                    {
                        fscreate.Close();
                    }
                }
                using (FileStream fs = new FileStream(path,FileMode.Append,FileAccess.Write,FileShare.ReadWrite))
                {
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        sw.Write(DateTime.Now.ToString("HH:mm:ss") + " " + txt + "\r\n");
                        sw.Close();
                    }
                    fs.Close();
                }
            }

            catch (Exception ex)
            {
                WriteLog("程序发生异常（WriteLog）。详情：" + ex.Message);

            }

        }
    }
}
